package com.ysh.dao.system;


import com.ysh.domain.system.Dept;
import com.ysh.domain.system.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/*
    用户dao层接口
 */
public interface UserMapper {


    // 保存(添加功能)
    @Insert("insert into ss_user (user_id, email, user_name,  password, state, gender, telephone, birthday, join_date, dept_id)" +
            "  values (#{id}, #{email}, #{userName}," +
            "  #{password}, #{state}, #{gender}," +
            "  #{telephone}, #{birthday}, #{joinDate}," +
            "  #{deptId})")
    int save(User user);

    // 根据id查询
    @Select("select user_id, email, user_name,  password, state, gender, telephone, birthday, join_date, dept_id from ss_user where user_id=#{id}")
    @Results({
            @Result(id = true,column = "user_id",property = "id"),
            @Result(column = "email",property = "email"),
            @Result(column = "user_name",property = "userName"),
            @Result(column = "password",property = "password"),
            @Result(column = "state",property = "state"),
            @Result(column = "gender",property = "gender"),
            @Result(column = "telephone",property = "telephone"),
            @Result(column = "birthday",property = "birthday"),
            @Result(column = "join_date",property = "joinDate"),
            @Result(column = "dept_id",property = "deptId"),
            @Result(property = "dept",javaType = Dept.class,column = "dept_id",
                    one=@One(select = "com.ysh.dao.system.DeptMapper.findById")
            )
    })
    User findById(String id);

    // 查询所有
    @Select("select user_id, email, user_name, password, state, gender, telephone, birthday, join_date, dept_id from ss_user")
    @Results({
            @Result(id = true,column = "user_id",property = "id"),
            @Result(column = "email",property = "email"),
            @Result(column = "user_name",property = "userName"),
            @Result(column = "password",property = "password"),
            @Result(column = "state",property = "state"),
            @Result(column = "gender",property = "gender"),
            @Result(column = "telephone",property = "telephone"),
            @Result(column = "birthday",property = "birthday"),
            @Result(column = "join_date",property = "joinDate"),
            @Result(column = "dept_id",property = "deptId"),
            @Result(property = "dept",javaType = Dept.class,column = "dept_id",
                    one=@One(select = "com.ysh.dao.system.DeptMapper.findById")
            )
    })
    List<User> findAll();

    // 删除
    @Delete("delete from ss_user where user_id=#{id}")
    int delete(User user);

    // 修改
    @Update("update ss_user" +
            " set" +
            " user_name = #{userName}," +
            " state = #{state}," +
            " gender = #{gender}," +
            " telephone = #{telephone}," +
            " dept_id = #{deptId}" +
            " where user_id = #{id}")
    int update(User user);

}
